package com.moji.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.common.utils.DateUtils;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjHomeSolution;
import com.moji.entity.MjProduct;
import com.moji.entity.MjSolution;
import com.moji.entity.vo.SolutionVo;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjSolutionMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjSolutionService;
import com.moji.util.XMLAppendExample;
import org.apache.http.util.TextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 解决方案表(MjSolution)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:48
 */
@Service("mjSolutionService")
public class MjSolutionServiceImpl extends ServiceImpl<MjSolutionMapper, MjSolution> implements MjSolutionService {
    @Resource
    private MjSolutionMapper mjSolutionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjSolution queryById(Long id) {
        return this.mjSolutionDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<MjSolution> queryByPage() {
        LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(MjSolution::getSort);
        queryWrapper.orderByDesc(MjSolution::getUpdateTime);
        List<MjSolution> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjSolution insert(MjSolution mjSolution) {
        LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjSolution::getRouter,mjSolution.getRouter());
        List<MjSolution> mjSolutions = mjSolutionDao.selectList(queryWrapper);
        if(mjSolutions!=null&&mjSolutions.size()>0)
        {
          return null;
        }
        //更新sitemap.xml文件
        String url="http://47.93.153.16/solution/qixiang?types="+mjSolution.getRouter();
        String date= DateUtils.formatDate(new Date(),DateUtils.DATE_FORMAT);
        XMLAppendExample.addXml(url,date,2);
        this.mjSolutionDao.insert(mjSolution);
        return mjSolution;
    }

    /**
     * 修改数据
     *
     * @param mjSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjSolution update(MjSolution mjSolution) {
        LambdaQueryWrapper<MjSolution> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(MjSolution::getRouter,mjSolution.getRouter());
        List<MjSolution> mjSolutions = mjSolutionDao.selectList(queryWrapper1);
        if(mjSolutions!=null&&mjSolutions.size()>0)
        {
            if(mjSolutions.get(0).getId()!=mjSolution.getId())
            {
                return null;
            }

        }
        LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjSolution::getId,mjSolution.getId());
        this.mjSolutionDao.update(mjSolution,queryWrapper);
        return this.queryById(mjSolution.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjSolutionDao.deleteById(id) > 0;
    }

    @Override
    public List<SolutionVo> solutionSelect() {
        List<SolutionVo> solutionVos=new ArrayList<>();
        List<MjSolution> solutionList=this.list();
        if(ObjectUtil.isNotEmpty(solutionList)){
            solutionList.forEach(item->{
                SolutionVo solutionVo=new SolutionVo();
                BeanUtils.copyProperties(item,solutionVo);
                solutionVos.add(solutionVo);
            });
        }
        return solutionVos;
    }
}
